package de.bbl.android.classes;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import de.bbl.android.tools.Tools;

public class Match {
	
	private int matchID;
	private int matchDayNumber;
	private Date matchDate;
	private String matchDateString;
	private String matchTimeString;
	private int competitionID;
	private String period;
	private PlayingTeam home;
	private PlayingTeam guest;
	
	public Match(int mId, int dayNumber, String date, int compID, String periodStr, PlayingTeam homePT, PlayingTeam guestPT) {
		matchID = mId;
		matchDayNumber = dayNumber;
		SetDate(date);
		competitionID = compID;
		period = periodStr;
		home = homePT;
		guest = guestPT;
	}
	
	private void SetDate(String date) {
		try {
			String pattern = "yyyy-MM-dd'T'HH:mm:ss";
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
			matchDate = (Date)sdf.parse(date);
		} catch (Exception e) {
			matchDate = new Date();
        	Tools.AppendToErrorFile(e.getMessage());
            // e.printStackTrace();
		}		
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
		sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
		matchDateString = sdf.format(matchDate);
		sdf = new SimpleDateFormat("HH:mm");
		sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
		matchTimeString = sdf.format(matchDate);
	}
	
	@Override public String toString() {
		return "Match: ID: " + matchID + ", Spieltag: " + matchDayNumber + ", Datum: " + matchDateString + " " + matchTimeString + "CompetitionID: " + competitionID + " Viertel: " + period + "Hometeam: " + home.toString() + "; Awayteam: " + guest.toString(); 
	}
	
	public String getHomeTeam() {
		return home.getTeamName();
	}

	public String getAwayTeam() {
		return guest.getTeamName();
	}

	public String getPeriod() {
		return period;
	}

	public String getDate() {
		return matchDateString;
	}
	
	public String getTime() {
		return matchTimeString;
	}

	public int getHomeTeamId(){
		return home.getTeamID();
	}

	public int getAwayTeamId(){
		return guest.getTeamID();
	}

	public String getScores() {
		return String.valueOf(home.getResult()) + " : " + String.valueOf(guest.getResult());
	}
}
